﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfChartingControlLibrary.PieChart
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfChartingControlLibrary.Themes"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfChartingControlLibrary.Themes;assembly=WpfChartingControlLibrary.Themes"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:PieSlice/>
    ///
    /// </summary>
    public class PieSlice : Control
    {
        
        private static void ShowValueLabelsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            PieSlice bcc = (PieSlice)sender;
            if (e.Property == PieSliceEffectProperty)
            {
                bcc.Effect = (Effect)e.NewValue;
            }
        }


        #region public Geometry Geometry
        /// <summary>
        /// Gets or sets the Geometry property which defines the shape of the
        /// data point.
        /// </summary>
        public Geometry Geometry
        {
            get { return GetValue(GeometryProperty) as Geometry; }
            set { SetValue(GeometryProperty, value); }
        }

        /// <summary>
        /// Identifies the Geometry dependency property.
        /// </summary>
        public static readonly DependencyProperty GeometryProperty =
            DependencyProperty.Register(
                "Geometry",
                typeof(Geometry),
                typeof(PieSlice),
                null);
        #endregion public Geometry Geometry


        #region public Fill
        /// <summary>
        /// Gets or sets the Geometry property which defines the shape of the
        /// data point.
        /// </summary>
        public Brush Fill
        {
            get { return GetValue(FillProperty) as Brush; }
            set { SetValue(FillProperty, value); }
        }

        /// <summary>
        /// Identifies the Geometry dependency property.
        /// </summary>
        public static readonly DependencyProperty FillProperty =
            DependencyProperty.Register(
                "Fill",
                typeof(Brush),
                typeof(PieSlice),
                null);
        #endregion public Fill


        #region public pieSliceEffect
        /// <summary>
        /// Gets or sets the Geometry property which defines the shape of the
        /// data point.
        /// </summary>
        public Effect PieSliceEffect
        {
            get { return GetValue(PieSliceEffectProperty) as Effect; }
            set { SetValue(PieSliceEffectProperty, value); }
        }

        /// <summary>
        /// Identifies the Geometry dependency property.
        /// </summary>
        public static readonly DependencyProperty PieSliceEffectProperty =
            DependencyProperty.Register(
                "PieSliceEffect",
                typeof(Effect),
                typeof(PieSlice),
                null);
        #endregion public Fill


        static PieSlice()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PieSlice), new FrameworkPropertyMetadata(typeof(PieSlice)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _path = GetTemplateChild("dddd") as Path;
            _root = GetTemplateChild("Root") as Grid;
            _path.Loaded += _path_Loaded;
            _path.Unloaded += _path_Unloaded;
            _path.MouseEnter += _path_MouseEnter;
            _path.MouseLeave += _path_MouseLeave;
            _path.MouseDown += _path_MouseDown;
        }

        void _path_Unloaded(object sender, RoutedEventArgs e)
        {
   //         DataHide();
        }

        void _path_Loaded(object sender, RoutedEventArgs e)
        {
            DataInit();
        }


        bool isSelected = false;
        void _path_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!isSelected)
            {
                if (!Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.RightCtrl))
                {

                    /* Your code */ 
                }
                isSelected = true;                
                VisualStateManager.GoToState(this, "Selected", true);
                DataInit();
            }
            else
            {
                isSelected = false;
                VisualStateManager.GoToState(this, "Unselected", true);
            }

        }

        void _path_MouseLeave(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "Normal", true);
        }

        void _path_MouseEnter(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "MouseOver", true);
        }


        public void DataInit()
        {
            VisualStateManager.GoToState(this, "Shown", true);
        }

        public void DataHide()
        {
            VisualStateManager.GoToState(this, "Hidden", true);
        }

        Path _path;
        Grid _root;



    }
}
